<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Automobiles extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		// load library
		// load helper
		//$this->load->helper(array('url', 'form', 'html'));
		// load model
		$this->load->model('Automobile_model','',TRUE);
		$this->load->model('Autobrand_model','',TRUE);
		$this->load->model('Autotype_model','',TRUE);
		$this->load->model('Profile_model','',TRUE);
	}
	
	// load index file
	function index($offset=0) {
		$data['title'] = 'Automobile Listings';
		$data['message'] = '';
		$data['action'] = 'details';
		$limit = $this->config->item('per_page');
		//get_result($cond = NULL, $limit = 10, $offset = 0, $sort = NULL
		$cond['is_active'] = 1;
		$data['active'] = 'classifieds';
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Automobile_model->get_result($cond, $limit, $offset, $sort);
		$data['autolist'] = $results['rows'];
		$data['brands'] = $this->Autobrand_model->get_brands();
		$data['types'] = $this->Autotype_model->get_types();
		$data['num_rows'] = $this->Automobile_model->count_result($cond);;
		$data['active'] = 'automobiles';//for menu
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('automobiles/index');
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		
		$this->load->view('automobiles/index', $data);
	
	}
	function brands($brandid=1, $offset = 0)
	{
		//get_result($cond = NULL, $limit = 10, $offset = 0, $sort = NULL
		$cond['is_active'] = 1;
		$cond['brandID'] = $brandid;
		$limit = $this->config->item('per_page');
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Automobile_model->get_result($cond, $limit, $offset, $sort);
		
		$data['autolist'] = $results['rows'];
		$data['brands'] = $this->Autobrand_model->get_brands();
		$data['types'] = $this->Autotype_model->get_types();
		$data['num_rows'] = $this->Automobile_model->count_result($cond);
		$data['active'] = 'automobiles';//for menu
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('automobiles/index/brands/'. $brandid);
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$this->load->view('automobiles/index', $data);
	
	}
	
	// details advertisement
	function details($id){
		
		$data['title'] = 'Details advertisement';
		$data['message'] = '';
		$auto_id = (int) $id;
		
		$cond['automobileID'] = $auto_id;
		$results =$this->Automobile_model->get_result($cond);
		$autolist = $results['rows'];
		$auto_details = array();
		
		foreach ($autolist as $auto)
		{
			$auto_details = $auto;
		}

		$otherCond['is_active'] = 1;
		$otherCond['advertiserID'] = $auto_details->advertiserID;

		$otherads_result = $this->Automobile_model->get_result($otherCond, 4);
		$otherads = $otherads_result['rows'];
		//$auto_details = $autolist['rows'][0];
		$data['active'] = 'automobiles';
		$data['backurl'] = 'automobiles/';
		$data['brands'] = $this->Autobrand_model->get_brands();
		$data['types'] = $this->Autotype_model->get_types();
		$data['autodetails'] = $auto_details;
		$data['others'] = $otherads;
		$data['bookmark'] = $auto_details->title;
		$this->load->view('automobiles/details', $data);
		
	}
	
	//display based on browsing 
	function browse($keyword = NULL, $offset=0)
	{
		
		//$cond['brandID'] = (int) $id;
		$cond['is_active'] = 1;
		if ($keyword != null){
			$data = explode("_", $keyword);
			$column = $data[0];
			$value = $data[1];
			$cond[$column] = $value;
		}
		
		$limit = $this->config->item('per_page');
		$data['active'] = 'automobile';
		
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'asc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		$results = $this->Automobile_model->get_result($cond, $limit, $offset, $sort);
		$data['autolist'] = $results['rows'];
		$data['brands'] = $this->Autobrand_model->get_brands();
		$data['types'] = $this->Autotype_model->get_types();
		$data['num_rows'] = $this->Automobile_model->count_result($cond);;
		$data['active'] = 'automobiles';//for menu
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('automobiles/index');
		$config['total_rows'] = (int)$data['num_rows'];
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['autoBrands'] = get_autos_brand_count();
		$data['propTypes'] = get_property_type_count();
		$data['advTypes'] = get_classfieds_type_count();
		//to display chosen category
		$data['selectedcatg'] = $data['brands'][$value];
		if ($column == 'brandID'){
		$data['bookmark'] = $data['brands'][$value];
		}else{
			$data['bookmark'] = $this->getCategoryName((int)$value);
		}
		$this->load->view('automobiles/index', $data);
	}
	/**
	 * [This function is called when user submit search button]
	 * @param  [type] $keyword [description]
	 * @return [type]          [description]
	 */
	function search($offset=0)
	{
		$keyword = 0;
		if($this->input->post('keyword')){
			$keyword = $this->input->post('keyword');
			$data['keyword'] = $keyword;
		}
		$brand = 0;
		$type = 0;
		if ($this->input->post('brands')){
			$cond['brandID'] = (int)$this->input->post('brands');
			$brand = $this->input->post('brands');
			$data['brand'] = $brand;

		}
		if ($this->input->post('types')){
			$cond['typeID'] = (int) $this->input->post('types');
			$type = $this->input->post('types');
			$data['type'] = $type;

		}
			
			$limit = $this->config->item('per_page');
			$cond['is_active'] = 1;
			$sort =array( 
				array(
					'column'=>'posted_date',
					'order' => 'desc'
					),
				array(
					'column' => 'title',
					'order' => 'asc'
					),
			 );

			$results = $this->Automobile_model->get_search_result($keyword,$cond, $limit, $offset, $sort);
			//$this->form_data->keyword = $keyword;
			$data['autolist'] = $results['rows'];
			$data['brands'] = $this->Autobrand_model->get_brands();
			$data['types'] = $this->Autotype_model->get_types();
			$data['num_rows'] = $results['num_rows'];
			$data['active'] = 'automobiles';//for menu

			//pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url('automobiles/result') . '/'. str_replace(' ', '_', $keyword) . '/'. $brand. '/'. $type;
			$config['total_rows'] = (int)$data['num_rows'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 6;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['autoBrands'] = get_autos_brand_count();
			$data['propTypes'] = get_property_type_count();
			$data['advTypes'] = get_classfieds_type_count();
			$data['bookmark'] = 'search';
			$this->load->view('automobiles/index', $data);
		
	}

	function result ($keyword = 0, $brand = 0, $type=0, $offset = 0){
		if($keyword != 0){
				$keyword = str_replace('_', '', $keyword);
				$data['keyword'] = $keyword;
			}
		if ($brand != 0){
			$cond['brandID'] = (int) $brand;
			$data['brand'] = $cond['brandID'];

		}
		if ($type != 0){
			$cond['typeID'] = (int) $type;
			$data['type'] = $cond['typeID'];
		}
		$limit = $this->config->item('per_page');
			$cond['is_active'] = 1;
			$sort =array( 
				array(
					'column'=>'posted_date',
					'order' => 'desc'
					),
				array(
					'column' => 'title',
					'order' => 'asc'
					),
			 );

			$results = $this->Automobile_model->get_search_result($keyword,$cond, $limit, $offset, $sort);
			//$this->form_data->keyword = $keyword;
			$data['autolist'] = $results['rows'];
			$data['brands'] = $this->Autobrand_model->get_brands();
			$data['types'] = $this->Autotype_model->get_types();
			$data['num_rows'] = $results['num_rows'];
			$data['active'] = 'automobiles';//for menu
		
			//pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url('automobiles/result') . '/'. str_replace(' ', '_', $keyword) . '/'. $brand. '/'. $type;
			$config['total_rows'] = (int)$data['num_rows'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 6;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['autoBrands'] = get_autos_brand_count();
			$data['propTypes'] = get_property_type_count();
			$data['advTypes'] = get_classfieds_type_count();
			$data['bookmark'] = 'search';
			$this->load->view('automobiles/index', $data);
	}
	
	function getCategoryName ($choice = 0)
	{
		switch($choice){
			case 1:
				return 'Sell';break;
			case 2:
				return 'Buy';break;
			case 3:
				return 'Rent';break;
			default:
				break;
		}
	}

	
}